import Request from './req'
import type { RequestConfig } from '../types/index'

interface apiRequestConfig<T> extends RequestConfig {
  data?: T
}
interface apiResponse<T> {
  code: number
  message: string
  data: T
}

/**
 * @description: 函数的描述
 * @interface D 请求参数的interface
 * @interface T 响应结构的intercept
 * @param {apiRequestConfig} config 不管是GET还是POST请求都使用data
 * @returns {Promise}
 */
const apiRequest = <D, T = any>(config: apiRequestConfig<D>) => {
  const { method = 'GET' } = config
  if (method === 'get' || method === 'GET') {
    config.params = config.data
  }
  return Request.request<apiResponse<T>>(config)
}

export default apiRequest
